<template>
  <div class="home-container">
    <van-nav-bar class="nav-title">
      <van-button
        slot="title"
        type="info"
        class="search-btn"
        round
        icon="search"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
      ellipsis
      swipe-threshold="3"
    >
      <van-tab
        v-for="obj in channels"
        :key="obj.id"
        :title="obj.name"
      >
        <ArtList :channel="obj"></ArtList>
      </van-tab>
      <div
        slot="nav-right"
        class="placeholder"
      ></div>
      <div
        class="hamburger-btn"
        slot="nav-right"
        @click="show = !show"
      >
        <i
          slot="title"
          class="toutiao toutiao-gengduo"
        ></i>
      </div>
    </van-tabs>
    <van-popup
      v-model="show"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
      @close="isEdit.flag = false"
    >
      <ChannelEdit
        :channels="channels"
        :active="active"
        :isEdit="isEdit"
        @updateIndex="updateFn"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getArtTitleList } from '@/api/article'
import ArtList from '@/views/home/components/artList.vue'
import ChannelEdit from '@/views/home/components/channel-edit'
import { getItem } from '@/utils/storage'
import { mapState } from 'vuex'
export default {
  name: 'HomePage',
  components: {
    ArtList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      show: false,
      isEdit: {
        flag: false
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {
    user () {
      this.active = 0
      this.loadChannels()
    }
  },
  created () {
    this.loadChannels()
  },
  mounted () { },
  methods: {
    async loadChannels () {
      const localChannels = getItem('CHANNELS')
      if (this.user || !localChannels) {
        try {
          const { data } = await getArtTitleList()
          this.channels = data.data.channels
        } catch (err) {
          this.$toast('获取频道列表数据失败')
        }
      } else {
        this.channels = localChannels
      }
    },
    updateFn (index, flag = true) {
      this.active = index
      this.show = flag
    }
  }
}
</script>

<style scoped lang="less">
/deep/ .van-nav-bar__title {
  max-width: unset;
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
      color: #fff;
    }
  }
}
/deep/ .channel-tabs {
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    font-size: 30px;
    color: #777777;
  }
  .van-tab--active {
    color: #000;
  }
  .van-tabs__nav {
    padding-bottom: 0;
  }
  .van-tabs__line {
    bottom: 8px;
    width: 31px !important;
    height: 6px;
    background-color: #3296fa;
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: rgba(255, 255, 255, 0.902);
    .toutiao {
      font-size: 33px;
    }
    &::before {
      content: "";
      width: 1px;
      height: 82px;
      background: linear-gradient(white 15%, #c8c8c8, white 85%);
      position: absolute;
      top: 0;
      left: 0;
    }
  }
  .arts-list {
    padding-bottom: 100px;
  }
}
/deep/ .van-icon-cross {
  color: #222;
}
</style>
